package cn.lg.soar.system.tenant.service.impl;

import cn.lg.soar.system.tenant.entity.Tenant;
import cn.lg.soar.system.tenant.mapper.TenantMapper;
import cn.lg.soar.system.tenant.service.ITenantService;
import cn.lg.soar.system.config.AuthCacheKey;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.Cacheable;

import java.io.Serializable;
import java.util.List;

/**
 * Tenant服务层实现
 * 租户
 * @author luguoxiang
 * @since 2022-04-30
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
public class TenantServiceImpl extends ServiceImpl<TenantMapper, Tenant> implements ITenantService {

    @Override
    public Tenant getById(Serializable id) {
        return super.getById(id);
    }

    @Override
    public boolean updateById(Tenant entity) {
        return super.updateById(entity);
    }

    @Override
    public boolean removeById(Serializable id) {
        return super.removeById(id);
    }

    @Cacheable(value = AuthCacheKey.TENANT_BY_USER, key = "#userId", cacheManager = "remote")
    @Override
    public List<Tenant> listBuUserId(Long userId) {
        return list(
                Wrappers.<Tenant>lambdaQuery()
                        .select(Tenant::getId, Tenant::getName)
                        .exists("select * from lg_sys_tenant_user r where lg_sys_tenant.id=r.tenant_id and r.user_id = " + userId)
                        .eq(Tenant::getDisabled, false)
        );
    }

}
